Skip to main content
OpenCLIP provides tools to easily upload your trained models to the Hugging Face Hub. This makes your models discoverable, shareable, and easy to load for others using the OpenCLIP library.

Overview

The push_to_hf_hub module provides:
  • Command-line tool for uploading models
  • Python API for programmatic uploads
  • Automatic configuration file generation
  • Model card creation
  • Support for safetensors format

Installation

Ensure you have the required dependencies:
Login to Hugging Face:
Or provide a token directly in the command.

Command-Line Usage

Use the push_to_hf_hub module as a command-line tool:

Required Parameters

  • --model: Name of the model architecture (e.g., ViT-B-32, ViT-L-14)
  • --pretrained: Path to checkpoint file or pretrained tag
  • --repo-id: Hugging Face Hub repository ID (format: username/repo-name)

Optional Parameters

  • --precision: Model precision (fp32, fp16, bf16) - default: fp32
  • --image-mean: Override image mean values for preprocessing
  • --image-std: Override image std values for preprocessing
  • --image-interpolation: Image resize interpolation method (bicubic, bilinear)
  • --image-resize-mode: Image resize mode (shortest, longest, squash)
  • --hf-tokenizer-self: Make tokenizer config point to the uploaded model itself

Examples

Example 1: Upload Trained Model

Upload a model you trained locally:

Example 2: Upload with Custom Preprocessing

Upload a model with custom preprocessing parameters:

Example 3: Re-upload Existing Model

Re-upload an existing OpenCLIP model to your Hub:
This example is from the README - uploading a ConvNeXt model trained on LAION-2B.

Example 4: Upload with Self-Referencing Tokenizer

Upload a model with custom tokenizer that references itself:
The --hf-tokenizer-self flag makes the tokenizer configuration point to the uploaded model repository instead of the original tokenizer source.

Python API

You can also upload models programmatically:

Basic Upload

Upload with Custom Configuration

Upload with Model Card

""", ‘license’: ‘mit’, } push_pretrained_to_hf_hub( model_name=‘ViT-B-32’, pretrained=‘/path/to/checkpoint.pt’, repo_id=‘myusername/my-clip-model’, model_card=model_card, )

Manual Upload with Custom Files

What Gets Uploaded

When you push a model to the Hub, the following files are created:

Model Weights

  • open_clip_pytorch_model.bin: PyTorch weights (pickle format)
  • open_clip_model.safetensors: SafeTensors weights (recommended)

Configuration

  • open_clip_config.json: Model architecture and preprocessing configuration

Tokenizer Files

  • tokenizer_config.json: Tokenizer configuration
  • vocab.json, merges.txt: Tokenizer vocabulary (for BPE tokenizers)
  • Other tokenizer-specific files

Model Card

  • README.md: Automatically generated model card with metadata

Loading Uploaded Models

Once uploaded, anyone can load your model:
See the Loading Models guide for more details.

Model Card Customization

Create comprehensive model cards for better documentation:

Best Practices

  1. Use Descriptive Repo Names
  2. Include Training Information
    • Dataset name and size
    • Training duration
    • Key hyperparameters
    • Performance metrics
  3. Provide Usage Examples
    • Include code snippets in model card
    • Show both inference and fine-tuning
    • Document any special requirements
  4. Use SafeTensors Format
  5. Version Your Models
    • Use tags or branches for different versions
    • Document changes between versions
  6. Test Before Uploading
  7. Add Relevant Tags

Troubleshooting

Authentication Error

Solution: Login to Hugging Face Hub

Repository Already Exists

Solution: The repository name is already taken. Choose a different name or use your existing repo.

Large File Upload Issues

Solution: Ensure git-lfs is installed

Missing Configuration

Solution: Ensure the model name is correct and the config exists:

Tokenizer Issues

Solution: For custom tokenizers, ensure the tokenizer files are included or use --hf-tokenizer-self.

Example Workflow

Complete workflow from training to Hub upload:

Additional Resources